package com.neko.seed.util;

//import com.sun.deploy.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 *文件相关操作
 * @author monik
 */
public class FileUtils {

    public static void downloadFile(HttpServletResponse response, File file, String newFileName){
        try {
            //设置中文文件名与后缀
//            String encodedFileName = URLEncoder.encode(newFileName,"utf-8").replaceAll("\\+", "%20");
            // 清除buffer缓存
            response.setHeader("Content-Disposition", "attachment; filename=" + newFileName + "");
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            InputStream is = new FileInputStream(file.getAbsoluteFile());
            BufferedInputStream bis = new BufferedInputStream(is);
            int length = 0;
            byte[] temp = new byte[1 * 1024 * 10];
            while ((length = bis.read(temp)) != -1){
                bos.write(temp, 0 ,length);
            }
            bos.flush();
            bis.close();
            bos.close();
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
